﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using BLL.教师操作BLL;
using Model;

namespace NCRE学生考试端V1._0.教师端操作类
{
    public partial class frmIPManagement : Form
    {
        public frmIPManagement()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 12:07:04
        private static frmIPManagement FrmIPManagement = null;
        public static frmIPManagement GetInstance()
        {
            if (FrmIPManagement==null|| FrmIPManagement.IsDisposed)
            {
                FrmIPManagement = new frmIPManagement();
            }
            return FrmIPManagement;
        }
        #endregion

        SelectIpBLL selectIp = new SelectIpBLL();

        DataTable table1 = new DataTable();

        

        #region 窗体加载
        private void frmIPManagement_Load(object sender, EventArgs e)
        {            
            table1 = selectIp.SelectAllIp();

            cRows = table1.Rows.Count;
            cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
            PageCurrent = 1;
            record = cRows;
            LoadData();

            dataGridView1.Columns[0].HeaderText = "考场ID";
            dataGridView1.Columns[1].HeaderText = "考场";
            dataGridView1.Columns[2].HeaderText = "ip段";
            dataGridView1.Columns[3].HeaderText = "ip起始段";
            dataGridView1.Columns[4].HeaderText = "ip截至段";

            //设定考场列只可读取，不可修改
            this.dataGridView1.Columns[0].ReadOnly = true;
            this.dataGridView1.Columns[1].ReadOnly = true;

            //在datagridview中添加button按钮
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            btn.Name = "btnModify";
            btn.HeaderText = "确认修改";
            btn.DefaultCellStyle.NullValue = "确认修改";
            dataGridView1.Columns.Add(btn);                       
        }
        #endregion

        #region datagridview里的button按钮事件           
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //点击button按钮事件
            if (dataGridView1.Columns[e.ColumnIndex].Name == "btnModify" && e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                IPEntity ipentity = new IPEntity();

                ipentity.ipID = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
                ipentity.ipName = Convert.ToString(dataGridView1.CurrentRow.Cells[1].Value);
                ipentity.ipPart = Convert.ToString(dataGridView1.CurrentRow.Cells[2].Value);
                ipentity.ipStart = Convert.ToString(dataGridView1.CurrentRow.Cells[3].Value);
                ipentity.ipEnd = Convert.ToString(dataGridView1.CurrentRow.Cells[4].Value);
                               
                bool flag = selectIp.UpdateIP(ipentity);
                if (flag)
                {
                    MessageBox.Show("更新成功！");
                }
                else
                {
                    MessageBox.Show("更新失败！");
                }
            }           
        }
        #endregion

        #region 添加新的考场-吕德彪-2018
        private void btnOK_Click(object sender, EventArgs e)
        {
            
            if(txtIpName.Text==""|| txtIpPart.Text==""|| txtIpStart.Text==""|| txtIpEnd.Text=="")
            {
                MessageBox.Show("新增教室IP对应数据不能为空，请重新输入！");
                txtIpName.Text = "";
                txtIpPart.Text = "";
                txtIpStart.Text = "";
                txtIpEnd.Text = "";
                txtIpName.Focus();
            }
            else
            {
                IPEntity ipentity = new IPEntity();
                ipentity.ipName = txtIpName.Text.Trim();
                ipentity.ipPart = txtIpPart.Text.Trim();
                ipentity.ipStart = txtIpStart.Text.Trim();
                ipentity.ipEnd = txtIpEnd.Text.Trim();
                bool flag = selectIp.InsertIP(ipentity);
                if (flag)
                {
                    MessageBox.Show("插入成功！");
                    txtIpName.Text = "";
                    txtIpPart.Text = "";
                    txtIpStart.Text = "";
                    txtIpEnd.Text = "";
                    #region 刷新datagridview
                    table1 = selectIp.SelectAllIp();

                    cRows = table1.Rows.Count;
                    cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                    PageCurrent = 1;
                    record = cRows;
                    LoadData();
                    #endregion
                }
                else
                {
                    MessageBox.Show("插入失败！");
                }
            }
            

            
        }
        #endregion

        #region 分页功能-吕德彪-2018
        
        //定义分页需要的变量
        int cRows = 0;
        int RowCurrent = 0;
        int cPages = 0;
        int PageCurrent = 0;
        const int PageSize = 7;
        int record = 0;


        private void LoadData()
        {
            if (PageCurrent == 1)
            {
                ts_PagePrevious.Enabled = false;
            }
            else
            {
                ts_PagePrevious.Enabled = true;
            }
            if (cPages == PageCurrent)
            {
                ts_PageNext.Enabled = false;
            }
            else
            {
                ts_PageNext.Enabled = true;
            }
            toolStripLabel4.Text = record.ToString();
            ts_PagePostion.Text = PageCurrent.ToString();
            ts_Pages.Text = cPages.ToString();
            int startRow = (PageCurrent - 1) * PageSize;
            int endRow = PageCurrent == cPages ? cRows : PageCurrent * PageSize;
            DataTable dtTemp = table1.Clone();
            if (table1.Rows.Count != 0)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    dtTemp.ImportRow(table1.Rows[i]);
                }
                bindingSource1.DataSource = dtTemp;
                dataGridView1.DataSource = bindingSource1;

            }

        }

        private void bindingNavigator1_ItemClicked_1(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                ts_PageNext.Enabled = true;
                if (PageCurrent == 1)
                {
                    ts_PagePrevious.Enabled = false;
                    MessageBox.Show("已经是第一页了");
                    return;
                }
                PageCurrent--;
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                ts_PagePrevious.Enabled = true;
                if (PageCurrent == PageSize)
                {
                    ts_PageNext.Enabled = false;
                    MessageBox.Show("已经是最后一页了");
                    return;
                }
                PageCurrent++;
                LoadData();
            }
        }

        private void bindingNavigatorMoveFirstPage_Click_1(object sender, EventArgs e)
        {
            PageCurrent = 1;
            bindingNavigatorMoveFirstPage.Enabled = false;
            bindingNavigatorMoveLastPage.Enabled = true;
            ts_PagePrevious.Enabled = false;
            ts_PageNext.Enabled = true;
            LoadData();
        }

        private void bindingNavigatorMoveLastPage_Click_1(object sender, EventArgs e)
        {
            PageCurrent = cPages;
            bindingNavigatorMoveFirstPage.Enabled = true;
            bindingNavigatorMoveLastPage.Enabled = false;
            ts_PagePrevious.Enabled = true;
            ts_PageNext.Enabled = false;
            LoadData();
        }

        private void ts_PagePostion_TextChanged_1(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= cPages)
                {
                    PageCurrent = Convert.ToInt32(ts_PagePostion.Text);
                    LoadData();
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (System.Exception ex)
            {
                PageCurrent = 1;
                LoadData();
            }
        }
        #endregion

        #region 删除Ip
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //选中的行数  
                int iCount = dataGridView1.SelectedRows.Count;
                if (iCount < 1)
                {
                    MessageBox.Show("Delete Data Fail!", "Error", MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("是否删除选中的数据？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)  //循环遍历所有行  
                    {

                        if (true == this.dataGridView1.Rows[i].Selected)
                        {
                            //获取选中行的IPName
                            string s = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
                            //当前行处于选中状态，则将其删除  
                            this.dataGridView1.Rows.RemoveAt(i);
                            //删除库里信息
                            bool flag = selectIp.DeleteIP(s);
                            if (flag)
                            {
                                MessageBox.Show("删除成功！");
                               //重新加载datagridview中的数据
                                table1 = selectIp.SelectAllIp();

                                cRows = table1.Rows.Count;
                                cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                                PageCurrent = 1;
                                LoadData();

                                dataGridView1.Columns[0].HeaderText = "考场";
                                dataGridView1.Columns[1].HeaderText = "ip段";
                                dataGridView1.Columns[2].HeaderText = "ip起始段";
                                dataGridView1.Columns[3].HeaderText = "ip截至段";

                            }
                            else
                            {
                                MessageBox.Show("删除失败！");
                            }
                        }
                        
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }  
             
        }
        #endregion
    }
}
